package com.gwsdk.paysdk.acitvityV2;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gwsdk.paysdk.utils.AppStatus;
import com.gwsdk.paysdk.utils.AppStatusManager;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.TextUtils;

public class GWWebViewActivity extends GWBaseActivity {

    public static void openActivity(Activity activity,String url){

        Intent intent = new Intent(activity,GWWebViewActivity.class);
        intent.putExtra("url",url);
        activity.startActivity(intent);
    }

    private ProgressBar progressBar;
    private WebView webView;
    private TextView titleText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
//        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏(主要功能就是去除页面弹出时顶部黑色条)
        if(AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYVLE){
            Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            finish();
        }
        super.onCreate(savedInstanceState);
        setContentView(GWInflaterUtils.getLayout(this,"gw_activity_web_view"));
        webView = findViewById(GWInflaterUtils.getControl(this,"web_view"));
        progressBar = findViewById(GWInflaterUtils.getControl(this,"progress_bar"));
        titleText = findViewById(GWInflaterUtils.getControl(this,"title"));

        findViewById(GWInflaterUtils.getControl(this,"btn_back")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String url = getIntent().getStringExtra("url");

        if (webView.isHardwareAccelerated()) webView.setLayerType(View.LAYER_TYPE_HARDWARE,null);  //开启硬件加速
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        if (TextUtils.isEmpty(url)){
            webView.loadUrl("https://www.genwan.com");
        }else {
            if (url.startsWith("https://") || url.startsWith("http://")){
                webView.loadUrl(url);
            }else {
                webView.loadUrl("https://www.genwan.com");
            }
        }

        webView.setWebViewClient(webViewClient);
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressBar.setProgress(newProgress);
                if (newProgress == 100){
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            titleText.setText(webView.getTitle());
        }
    };

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            super.onBackPressed();
        }
    }

}
